<template>
      <view class="login-container">
        <uni-icons type="contact-filled" size="100" color="#afafaf"></uni-icons>
        <!-- open-type属性去掉，事件绑定改成@click="getUserProfile" -->
       <button type="primary" class="btn-login" @click="getUserProfile">一键登录</button>
        <text class="tips-text">登录后尽享更多权益</text>
      </view>
</template>

<script>
  import {mapMutations,mapState} from 'vuex';
  import request from "../../request/request.js";
	export default {
		data() {
			return {
				
			};
		},
    computed:{
      ...mapState('m_user',['redirectInfo'])
    },
    methods:{
      ...mapMutations('m_user',['updateUserInfo','updateRedirectInfo','saveJsCodeToStorage']),
      //用户授权之后,获取用户基本信息
      getUserProfile() {
		  let that = this;
		 that.wxLogo();
              uni.getUserProfile({
                desc: '你的授权信息',
                success: (res) => {
                  // 将信息存到 vuex 中
                  console.log(res);
				  console.info("zrf:"+that.js_code);
                  
                 // this.getToken(res)
				 this.InsertOpinID(res.userInfo);
				 
                },
                fail: (res) => {
                  return uni.$showMsg('您取消了登录授权')
                }
              })
      },
      wxLogo(){
      	let that  = this;
      		wx.login({
      		  success (res) {
      			if (res.code) {
      				that.js_code=res.code;
				uni.setStorageSync('js_code',JSON.stringify(that.js_code));
      				// this.saveJsCodeToStorage(res.code);
      			} else {
      			  console.log('登录失败！' + res.errMsg)
      			}
      		  }
      		})
      },
	  InsertOpinID(userInfo){
	  	let that  = this;
	  	let data =  {
	  		'wxName': userInfo.nickName,
	  		'headImage': userInfo.avatarUrl,
				// 'username': userInfo.nickName,
				// // 'password':userInfo.
	  		'js_code': that.js_code,
	  	}
	  	request('', '/User/insertOpinID', 'GET', data, {}).then(res => {
	  		console.log("首次请求返回结果",res)
	  		if (res.code == 200) {
	  		
	  		//此部设置用户信息,
	  		// that.userInfo.nickName = res.data.wxName;
	  		// that.userInfo.avatarUrl = res.data.headImage;
	  		// that.userInfo.answeUserOpenid=res.data.userOpenid;
	  		//设置已经登陆的标志
	  		// that.isLoginStatus = true;
	  		uni.setStorageSync('tbAnsweUser',res.data );
			uni.setStorageSync("accessToken",res.data.accessToken);
			uni.setStorageSync("refreshToken",res.data.refreshToken);
			uni.setStorageSync("sessionId",res.data.sessionId);
			this.updateUserInfo(res.data);
	  		} else{
	  			uni.showToast({
	  				title: '请求错误',
	  				icon: 'error',
	  				duration: 2000
	  			});
	  		}
	  		uni.hideLoading();
	  	})	
	  },
      navigateBack(){
        if(this.redirectInfo && this.redirectInfo.onpenType === 'switchTab'){
          uni.switchTab({
            url:this.redirectInfo.from,
            complete:()=>{
              this.updateRedirectInfo(null)
            }
          })
        }
      }
    }
	}
</script>

<style lang="scss">
.login-container{
  height: 750rpx;
  background-color: #f8f8f8;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
  
  &::after{
    content: '';
    display: block;
    width: 100%;
    height: 40px;
    background-color: white;
    position: absolute;
    bottom: 0;
    left: 0;
    border-radius: 100%;
    transform: translateY(50%);
  }
  .btn-login{
    width: 90%;
    border-radius: 100px;
    margin: 15px 0;
    background-color: #4169E1;
  }
  .tips-text{
    font-size: 12px;
    color: gray;
  }
}
</style>
